<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Blog\Lists;
use Illuminate\Http\Request;

class ListService
{

    use Service;

    /**
     * 博客菜单首页
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function ListIndexService( $request )
    {
        $select = [ 'id', 'name', 'sort_order', 'is_show', 'status' ];
        $list   = Lists::GetModel([ 'status' => 1 ], [], $select);
        return view('blog.list.index', compact('list'));
    }

    /**
     * 博客菜单新增
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function ListStoreService( $request )
    {
        $validator = $this->getAdminValidator()->ListValidator($request);
        if ( $validator->first() ) return back()->with('error', $validator->first());
        Lists::CreateModel($request->all());
        return back()->with('success', $this->getErrorCode()::message(209));
    }

    /**
     * 博客菜单编辑页面
     *
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function ListEditService( $id )
    {
        $select = [ 'id', 'name', 'sort_order', 'is_show', 'status' ];
        $list   = Lists::FindModel($id, $select);
        return view('blog.list.edit', compact('list'));
    }

    /**
     * 博客菜单更新
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function ListUpdateService( $request )
    {
        $validator = $this->getAdminValidator()->ListUpdateValidator($request);
        if ( $validator->first() ) return back()->with('error', $validator->first());
        $select = [ 'id', 'name', 'sort_order', 'is_show', 'status' ];
        $list   = Lists::FindModel($request->input('id'), $select);
        $list->update($request->all());
        return back()->with('success', $this->getErrorCode()::message(203));
    }

    /**
     * 博客菜单删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function ListDestroyService( $id )
    {
        Lists::DestroyModel($id);
        return back()->with('success', $this->getErrorCode()::message(204));
    }

    /**
     * 修改博客菜单是否显示
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function ListStatusService( $id )
    {
        $select = [ 'id', 'is_show' ];
        $list   = Lists::FindModel($id, $select);
        if ( $list->is_show == 1 ) $is_show = 2;
        else $is_show = 1;
        $list->is_show = $is_show;
        $list->save();
        return back()->with('success', $this->getErrorCode()::message(210));
    }

    /**
     * 博客菜单排序
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function ListSortOrderService( $request )
    {
        $array = json_decode($request->input('sort'), TRUE);
        if ( ! ( new CommonService() )->sort($array, 'list', FALSE, 2, 1) ) return message($this->getErrorCode()::message(2196));
        return data();
    }

}
